package com.example.springboot;

import com.example.springboot.config.DefaultProfileUtil;
import com.example.springboot.domain.User;
import io.github.jhipster.config.JHipsterConstants;
import lombok.extern.slf4j.Slf4j;
import net.logstash.logback.encoder.org.apache.commons.lang3.StringUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.core.env.Environment;

import javax.annotation.PostConstruct;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Collection;


@Slf4j
@EnableFeignClients(basePackages = {"com.example.springboot"})
@SpringBootApplication
public class SpringbootApplication {

    private final Environment env;

    public SpringbootApplication(Environment env) {
        this.env = env;
    }

    public static void main(String[] args) {
        //SpringApplication.run(SpringbootApplication.class, args);
        try {
            SpringApplication app = new SpringApplication(SpringbootApplication.class);
            DefaultProfileUtil.addDefaultProfile(app);
            Environment env = app.run(args).getEnvironment();
            logApplicationStartup(env);
        } catch (Exception e) {
            log.error("", e);
        }

        //app.run(args);
    }

    private static void logApplicationStartup(Environment env) {

        String protocol = "http";
        if (env.getProperty("server.ssl.key-store") != null) {
            protocol = "https";
        }
        String serverPort = env.getProperty("server.port");
        String contextPath = env.getProperty("server.servlet.context-path");
        if (StringUtils.isBlank(contextPath)) {
            contextPath = "/";
        }
        String hostAddress = "localhost";
        try {
            hostAddress = InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            log.warn("主机名无法确定，使用 localhost 作为后备");
        }
        log.info("\n----------------------------------------------------------\n\t" +
                        "应用:'{}' 正在运行! 访问地址为:\n\t" +
                        "本地地址: \t{}://localhost:{}{}\n\t" +
                        "外部地址: \t{}://{}:{}{}\n\t" +
                        "环境: \t\t{}\n----------------------------------------------------------",
                env.getProperty("spring.application.name"),
                protocol,
                contextPath,
                serverPort,
                protocol,
                hostAddress,
                serverPort,
                contextPath,
                DefaultProfileUtil.getActiveProfiles(env));

        String configServerStatus = env.getProperty("configserver.status");
        if (configServerStatus == null) {
            configServerStatus = "找不到或没有设置配置服务器";
        }
        log.info("\n----------------------------------------------------------\n" +
                "配置服务器: {}\n----------------------------------------------------------", configServerStatus);
    }

    /**
     * 初始化项目
     * 可以使用程序参数来配置Spring配置文件 --spring.profiles.active = your-active-profile(您的活动配置文件)
     * 您可以在以下位置找到有关配置文件如何与JHipster一起工作的更多信息：
     * https://www.jhipster.tech/profiles/
     */
    @PostConstruct
    public void initApplication() {
        Collection<String> activeProfiles = Arrays.asList(env.getActiveProfiles());
        if (activeProfiles.contains(JHipsterConstants.SPRING_PROFILE_DEVELOPMENT) &&
                activeProfiles.contains(JHipsterConstants.SPRING_PROFILE_PRODUCTION)) {
            log.error("您配置了错误的应用程序！它不应该运行，同时具有 dev 和 prod 配置文件！");
        }
        if (activeProfiles.contains(JHipsterConstants.SPRING_PROFILE_DEVELOPMENT) &&
                activeProfiles.contains(JHipsterConstants.SPRING_PROFILE_CLOUD)) {
            log.error("您配置了错误的应用程序！它不应该运行，同时使用 dev 和 cloud 配置文件运行！");
        }
    }
}
